package com.mbigger.admin.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.mbigger.admin.entity.Role;

@Mapper
public interface RoleMapper {
	
	@Insert("INSERT INTO sys_role (name, type, status, remark, operator, operate_time, operate_ip) "
			+ "VALUES (#{name}, #{type}, #{status}, #{remark}, #{operatoe}, #{operateTime}, #{operateIp})")
	int insert(Role role);
	
	@Delete("DELETE FROM sys_role WHERE id=#{id}")
	int deleteById(@Param("id")Integer id);

	@Select("SELECT * FROM sys_role WHERE id=#{id}")
	Role selectById(@Param("id")Integer id);

	@Update("UPDATE sys_role SET name=#{name}, type=#{type}, status=#{status}, remark=#{remark}, operator=#{operator}, operate_time=#{operateTime}, operate_ip=#{operateIp}")
	int updateById(Role role);
	
	@Select("SELECT * FROM sys_role")
	List<Role> getAll();
	
	@Select("<script>"
			+ "SELECT count(1) FROM sys_role WHERE name=#{name} "
			+ "<if test='id != null'>"
			+ "AND id!=#{id}"
			+ "</if>"
			+ "</script>")
	int countByName(@Param("name")String name, @Param("id")Integer id);
	
	@Select("<script>"
			+ "SELECT * FROM sys_role WHERE id IN "
			+ "<foreach collection='idList' item='id' open='(' close=')' separator=','>"
			+ "#{id}"
			+ "</foreach>"
			+ "</script>")
	List<Role> getByIdList(@Param("idList")List<Integer> idList);
}
